class SceneGame extends eui.Component implements  eui.UIComponent {
	
	public btn_back:eui.Button;
	public img_question:eui.Image;
	public gp_answer:eui.Group;
	public gp_words:eui.Group;
	public gp_win:eui.Group;
	public btn_next:eui.Button;
	//自定义数据
	private cur_level:number=0; 

	public lab_cyjs:eui.Label;
	public lab_cycc:eui.Label;

	private static shared:SceneGame;
	public static getInstance(){
		if(!SceneGame.shared){
			SceneGame.shared=new SceneGame();
		}
		return SceneGame.shared;
	}

	public constructor() {
		super();
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		super.childrenCreated();
		this.init();
	}
	private init(){
		this.btn_back.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTabBackBtn,this);
		this.btn_next.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTabNext,this);
	}
	//初始化游戏场景
	public initLevelData(level:number){
		//记录当前关卡
		this.cur_level=level;
		//获取本关卡数据
		let curLevelData:LevelDataItem=LevelDataManager.getInstance().GetLevel(level);
		//设置问题的图片
		this.img_question.source="resource/assets/data"+curLevelData.img;
		//设置问题字  当前关卡的成语（四个字）和混淆字（六个字）拼接起来
		let words:string=curLevelData.answer+curLevelData.word;
		//拼接20个字符
		while(words.length==10){
			let i=Math.floor(Math.random()*LevelDataManager.getInstance().totalLevels);
			if(i!=level){
				let randomLevelData:LevelDataItem=LevelDataManager.getInstance().GetLevel(level);
				words+=(randomLevelData.word+randomLevelData.answer);
			}
		}
		//把words变成数组
		let wordsArr=words.split("");
		wordsArr=this.randomList(wordsArr);
		for(let i:number=0;i<this.gp_words.numChildren;i++){
			let gp_world_item=<Word>this.gp_words.getChildAt(i);
			gp_world_item.setWordText(wordsArr[i]);
			gp_world_item.visible=true;
		}
		for(let i:number=0;i<this.gp_answer.numChildren;i++){
			let gp_answer_item=<AnswerWord>this.gp_answer.getChildAt(i);
			gp_answer_item.setSelectWord(null);
			gp_answer_item.select_word=null;
			gp_answer_item.visible=true;
		}
	}
	private onTabBackBtn(){
		this.parent.addChild(SceneLevel.Shared());
		this.parent.removeChild(this);
	}
	//下一题
	private onTabNext(){
		//隐藏正确面板
		this.gp_win.visible=false;
		SceneLevel.Shared().setMileStoneLevel(this.cur_level+1);
		//重新初始化游戏场景
		this.initLevelData(this.cur_level+1);
	}
	//文字的回调函数
	public onTabSelectWord(w:Word){
		SoundManager.getInstance().playClick();
		let sel:AnswerWord=null;
		for(let i=0;i<this.gp_answer.numChildren;i++){
			let answerWord=<AnswerWord>this.gp_answer.getChildAt(i);
			if(answerWord.getWordText()=="")
			{
				sel=answerWord;
				break;
			}
		}
		//找到空白文字
		if(sel){
			sel.setSelectWord(w);
		}
		let answerStr:string="";
		for(let i=0;i<this.gp_answer.numChildren;i++){
			let gpAnswerItem=<AnswerWord>this.gp_answer.getChildAt(i);
			answerStr+=gpAnswerItem.getWordText();
		}
		if(answerStr.length==4){
			if(answerStr==LevelDataManager.getInstance().GetLevel(this.cur_level).answer){
				SoundManager.getInstance().playRight();
				this.showWin();
			}else{
				SoundManager.getInstance().playWrong();
			}
		}
	}
	private showWin(){
		let data:LevelDataItem=LevelDataManager.getInstance().GetLevel(this.cur_level);
		this.lab_cyjs.text=data.tip;
		this.lab_cycc.text=data.content;
		this.gp_win.visible=true;
	}
	//对一个数组进行随机排列
	private randomList(arr:string[]):string[]{
		var array=[];
		while(arr.length>0){
			var i = Math.floor(Math.random()*arr.length);
			array.push(arr[i]);
			arr.splice(i,1); 
		}
		return array;
	}
}